package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.AdItem;
import cn.nubia.admin.application.bean.AdSpace;
import cn.nubia.admin.application.dto.AdItemDTO;
import cn.nubia.admin.application.query.AdItemQuery;
import cn.nubia.admin.application.service.AdItemService;
import cn.nubia.admin.application.service.AdSpaceService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * Created by cnJason on 2015/7/6.
 * 广告位内容界面操作层.
 */
@Controller
@RequestMapping("/admin/ad")
public class AdItemController {


    @Autowired
    private AdItemService adItemService;

    @Autowired
    private AdSpaceService adSpaceService;

    /**
     * 查询广告位列表
     *
     * @param adTemQuery 广告位查询参数
     * @return 广告位列表.
     */
    @RequiresPermissions(value = "soft:ad:adItemList")
    @RequestMapping(value = "adItemList")
    public ModelAndView getAdItemList(AdItemQuery adTemQuery) {
        ModelAndView modelAndView = new ModelAndView("/ad/adItemList");
        Paged<AdItemDTO> adItemPage = adItemService.findPage(adTemQuery);
        modelAndView.addObject("adItemList", adItemPage);
        modelAndView.addObject("query", adTemQuery);
        return modelAndView;
    }


    /**
     * 保存广告位内容
     *
     * @param adItem 广告位内容
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveAdItem", method = RequestMethod.POST)
    public Response<String> save(AdItem adItem) {
        return Response.getSuccessResponse(adItemService.save(adItem));
    }

    /**
     * 查看广告位内容
     *
     * @param adItemId 广告内容id
     * @return
     */
    @RequestMapping(value = "viewAdItem", method = RequestMethod.GET)
    public ModelAndView view(Integer adItemId, String origin) {

        ModelAndView modelAndView = new ModelAndView("/ad/adItemForm");
        AdItem adItem = new AdItem();
        List<AdSpace> adSpaceList = adSpaceService.findAll();
        AdSpace adSpace = new AdSpace();
        if (adItemId != null) {
            adItem = adItemService.get(adItemId);
        }


        modelAndView.addObject("adSpaceList", adSpaceList);
        modelAndView.addObject("origin", origin);
        modelAndView.addObject("adItem", adItem);
        modelAndView.addObject("adItemId", adItemId);
        return modelAndView;
    }

    /**
     * 删除广告位内容
     *
     * @param adItemId 广告内容id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteAdItem", method = RequestMethod.GET)
    public Response<Boolean> delete(Integer adItemId) {
        adItemService.changeAdItem(adItemId,AdItem.STATE_DEL);
        return Response.getSuccessResponse(true);
    }


    @ResponseBody
    @RequestMapping(value = "changeAdItem", method = RequestMethod.GET)
    public Response<Boolean> changeAdItem(Integer adItemId,Integer state) {
        adItemService.changeAdItem(adItemId,state);
        return Response.getSuccessResponse(true);
    }


}
